package articles

import (
	ArticleModel "blog/app/model/articles"
	articleRequest "blog/app/request/article"
	Core "blog/core"
	"errors"
	"strconv"
	"strings"

	"golang.org/x/exp/slices"
)

// GetList 获得文章列表
func (a *Article) GetList(param *articleRequest.Lists) ([]ArticleModel.ArticleList, int64) {
	var lists []ArticleModel.ArticleList
	var count int64
	Core.DbConnect.Model(a.ArticleModel).Order("id DESC").Limit(param.Limit).Offset((param.Page - 1) * param.Limit).Count(&count).Find(&lists)
	var cid []int = make([]int, 0)
	var tid []int = make([]int, 0)
	for _, v := range lists {
		if !slices.Contains(cid, v.Cid) {
			cid = append(cid, v.Cid)
		}

		ids := strings.Split(v.Tid, ",")
		for _, iv := range ids {
			if id, err := strconv.Atoi(iv); err == nil && !slices.Contains(tid, id) {
				tid = append(tid, id)
			}
		}
	}

	var cateModel *ArticleModel.Cate
	var cateList []ArticleModel.Cate
	var tagModel *ArticleModel.Tag
	var tagLists []ArticleModel.Tag
	Core.DbConnect.Model(cateModel).Select("name", "id").Find(&cateList, cid)
	Core.DbConnect.Model(tagModel).Select("name", "id").Find(&tagLists, tid)
	for k, v := range lists {
		for _, j := range cateList {
			if j.ID == v.Cid {
				lists[k].Cate = &j.Name
				break
			}
		}

		var tag []string
		for _, j := range tagLists {
			if strings.Contains(v.Tid, strconv.Itoa(j.ID)) {
				tag = append(tag, j.Name)
			}
		}

		lists[k].Tag = &tag
	}
	return lists, count
}

// GetInfo 获得文章信息
func (a *Article) GetInfo(id int) (*ArticleModel.ArticleInfo, error) {
	var info *ArticleModel.ArticleInfo
	row := Core.DbConnect.Model(a.ArticleModel).Find(&info, id).RowsAffected
	if row == 0 {
		return nil, errors.New("文章不存在")
	}
	Core.DbConnect.Model(a.ArticleCateModel).Find(&info.Cate, info.Cid)
	Core.DbConnect.Model(a.ArticleTagModel).Find(&info.Tag, strings.Split(info.Tid, ","))
	Core.DbConnect.Model(a.contentModel).Select("content").Where("aid = ?", info.ID).Find(&info.Content)
	return info, nil
}

// Create 添加新的文章
func (a *Article) Create(param *articleRequest.Create) error {
	info := ArticleModel.Article{
		Title:      param.Title,
		SubTitle:   param.SubTitle,
		Cid:        param.Cid,
		Tid:        param.Tid,
		IsPublish:  param.IsPublish,
		MainImg:    param.MainImg,
		IsComment:  param.IsComment,
		IsOriginal: param.IsOriginal,
	}

	result := Core.DbConnect.Create(&info)
	if err := result.Error; err != nil {
		return err
	}

	if param.Content != "" {
		Core.DbConnect.Create(&ArticleModel.Content{
			Aid:     info.ID,
			Content: param.Content,
		})
	}

	return nil
}

// Del 删除文章
func (a *Article) Del(id int) error {
	var count int64
	Core.DbConnect.Model(a.ArticleModel).Where("id = ?", id).Count(&count)
	if count == 0 {
		return errors.New("文章不存在")
	}

	return Core.DbConnect.Where("id = ?", id).Delete(&ArticleModel.Article{}).Error
}

// Edit 编辑
func (a *Article) Edit(id int, data map[string]interface{}) error {
	var count int64
	content := data["Content"].(string)
	delete(data, "Content")
	Core.DbConnect.Model(a.ArticleModel).Where("id = ?", id).Count(&count)
	if count == 0 {
		return errors.New("文章不存在")
	}

	if err := Core.DbConnect.Model(ArticleModel.Article{ID: id}).Updates(data).Error; err != nil {
		return err
	}

	if Core.DbConnect.Model(a.contentModel).Where("aid = ?", id).Count(&count); count > 0 {
		Core.DbConnect.Model(a.contentModel).Where("aid = ?", id).Update("content", content)
	} else {
		Core.DbConnect.Create(&ArticleModel.Content{
			Aid:     id,
			Content: content,
		})
	}

	return nil
}
